package com.dao.zk;

import com.bean.zk.Household;
import com.util.zk.Dbutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Repository
public class HousehodeDAO {

    /**
     * 根据用户名密码查询
     * @param num
     * @param pass
     * @return
     */
    public Map<String,Object> select(String num, String pass){
        Connection con = null;
        QueryRunner query = new QueryRunner();
        Map<String,Object>  household =null;
        try {
            con = Dbutil.getConnection();
            String sql = "select * from household where num = ? and pass = ?";
            Object[] args = new Object[]{num, pass};
            household = query.query(con, sql, new MapHandler(), args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return  household;
    }

    public  Map<String,Object>  selectByPhone(String phonenum) {

        Connection con = null;
        QueryRunner query = new QueryRunner();
        Map<String,Object> household =null;
        try {
            con = Dbutil.getConnection();
            String sql = "select * from household where htel = ?";
            household = query.query(con, sql, new MapHandler(), phonenum);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return household;
    }


    public int updatePswd(String pswd,String phonenum) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "update  household set pass = ? where htel = ?";
            Object[] args = new Object[]{pswd, phonenum};
            effect = query.update(con, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }

    /**
     * 查询所有的住户
     *
     * @return
     */
    public List<Household> getHouseholds() {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        List<Household>  household =null;
        try {
            con = Dbutil.getConnection();
            String sql = "select h.aid ,h.Htel , h.Hname,v.vname as vid ,t.ttname as tid , u.Uname as tnumber,ho.honame as hid ,h.HOpenid,h.HSm, h.Num,h.Pass from" +
                    " household h ,village v,tenement t,unit u,houses ho" +
                    " where h.vid = v.vid and t.tid = h.tid and u.Uid = h.tnumber and ho.hid = h.hid ";
            household = query.query(con, sql, new BeanListHandler<>(Household.class));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return  household;
    }


    public List<Household> getHousehold(Household ho) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        List<Household>  household =null;
        try {
            con = Dbutil.getConnection();
            String sql = " select h.aid ,h.Htel , h.Hname,v.vname as vid ,t.ttname as tid , u.Uname as tnumber,ho.honame as hid ,h.HOpenid,h.HSm, h.Num,h.Pass from " +
                    " household h ,village v,tenement t,unit u,houses ho " +
                    " where h.vid = v.vid and t.tid = h.tid and u.Uid = h.tnumber and ho.hid = h.hid " +
                    " and h.Hname like ? and h.Htel like ?";
            Object[] args = new Object[]{"%"+ho.getHname()+"%","%"+ho.getHtel()+"%"};
            household = query.query(con, sql, new BeanListHandler<>(Household.class),args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return  household;
    }
}
